﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cJSON.h>

void parse_cntv_data(char *str);
void parse_mi_data(char *str);

typedef struct medal{
    char rank[5];
    char countryname[50];
    char count[5];
    char gold[5];
    char silver[5];
    char bronze[5];
    char update_time[50];
    char countryid[10];
}medalObj;

int main(int argc, char *argv[])
{
    char *str_cntv = "{\"data\":{\"total\":84,\"medalsList\":[{\"bronze\":\"16\",\"rank\":\"1\",\"count\":\"69\",\"silver\":\"21\",\"countryname\":\"\\u4e2d\\u56fd\",\"gold\":\"32\",\"countryid\":\"CHN\"},{\"bronze\":\"21\",\"rank\":\"2\",\"count\":\"73\",\"silver\":\"28\",\"countryname\":\"\\u7f8e\\u56fd\",\"gold\":\"24\",\"countryid\":\"USA\"},{\"bronze\":\"11\",\"rank\":\"3\",\"count\":\"36\",\"silver\":\"6\",\"countryname\":\"\\u65e5\\u672c\",\"gold\":\"19\",\"countryid\":\"JPN\"},{\"bronze\":\"15\",\"rank\":\"4\",\"count\":\"33\",\"silver\":\"4\",\"countryname\":\"\\u6fb3\\u5927\\u5229\\u4e9a\",\"gold\":\"14\",\"countryid\":\"AUS\"},{\"bronze\":\"18\",\"rank\":\"5\",\"count\":\"52\",\"silver\":\"21\",\"countryname\":\"\\u4fc4\\u7f57\\u65af\\u5965\\u8fd0\\u961f\",\"gold\":\"13\",\"countryid\":\"ROC\"},{\"bronze\":\"13\",\"rank\":\"6\",\"count\":\"43\",\"silver\":\"17\",\"countryname\":\"\\u82f1\\u56fd\",\"gold\":\"13\",\"countryid\":\"GBR\"},{\"bronze\":\"14\",\"rank\":\"7\",\"count\":\"30\",\"silver\":\"8\",\"countryname\":\"\\u5fb7\\u56fd\",\"gold\":\"8\",\"countryid\":\"GER\"},{\"bronze\":\"8\",\"rank\":\"8\",\"count\":\"24\",\"silver\":\"10\",\"countryname\":\"\\u6cd5\\u56fd\",\"gold\":\"6\",\"countryid\":\"FRA\"},{\"bronze\":\"7\",\"rank\":\"9\",\"count\":\"20\",\"silver\":\"7\",\"countryname\":\"\\u8377\\u5170\",\"gold\":\"6\",\"countryid\":\"NED\"";
    char *str_mi = "{\"status\":0,\"message\":\"操作成功\",\"data\":[{\"country_name\":\"中国\",\"rank\":\"1\",\"country_flag\":\"https://si1.go2yd.com/get-image/0sO3K7OEEyP\",\"medal_gold_count\":\"36\",\"medal_silver_count\":\"26\",\"medal_bronze_count\":\"17\",\"medal_sum_count\":\"79\",\"update_time\":\"2021-08-07 09:47:01\"},{\"country_name\":\"美国\",\"rank\":\"2\",\"country_flag\":\"https://si1.go2yd.com/get-image/0sO3M0pmWd7\",\"medal_gold_count\":\"31\",\"medal_silver_count\":\"36\",\"medal_bronze_count\":\"32\",\"medal_sum_count\":\"99\",\"update_time\":\"2021-08-07 09:47:01\"},{\"country_name\":\"日本\",\"rank\":\"3\",\"country_flag\":\"https://si1.go2yd.com/get-image/0sO3KuhjKsK\",\"medal_gold_count\":\"24\",\"medal_silver_count\":\"11\",\"medal_bronze_count\":\"16\",\"medal_sum_count\":\"51\",\"update_time\":\"2021-08-07 09:47:01\"},{\"country_name\":\"英国\",\"rank\":\"4\",\"country_flag\":\"https://si1.go2yd.com/get-image/0sO3KX4x1yC\",\"medal_gold_count\":\"18\",\"medal_silver_count\":\"20\",\"medal_bronze_count\":\"20\",\"medal_sum_count\":\"58\",\"update_time\":\"2021-08-07 09:47:01\"},{\"country_name\":\"俄奥委会\",\"rank\":\"5\",\"country_flag\":\"https://si1.go2yd.com/get-image/0sO50PG6OdC\",\"medal_gold_count\":\"17\",\"medal_silver_count\":\"23\",\"medal_bronze_count\":\"22\",\"medal_sum_count\":\"62\",\"update_time\":\"2021-08-07 09:47:01\"},{\"country_name\":\"澳大利亚\",\"rank\":\"6\",\"country_flag\":\"https://si1.go2yd.com/get-image/0sO3Ji2IZN3\",\"me";

//    char *str = stdr_mi;
    char *str = str_cntv;

    char dest[2000];
    char *loc;
    memset(dest, '\0', 2000);
    loc = strrchr(str, '}');
    strncpy(dest, str, loc-str+1);
    strcat(dest, "]}}");

//    parse_mi_data(dest);
    parse_cntv_data(dest);

    return 0;
}

void parse_mi_data(char *str)
{
    cJSON *root, *data_obj, *list_obj;

    medalObj obj;
    medalObj *pobj;
    pobj = &obj;

    root = cJSON_Parse((const char*)str);
    if(root != 0)
    {
        printf("JSON format ok, start parse!!!\r\n");
        data_obj = cJSON_GetObjectItem(root, "data");
        if(data_obj->type == cJSON_Array)
        {
            int size = cJSON_GetArraySize(data_obj);
            for(int idx = 0; idx < size; idx++)
            {
                list_obj = cJSON_GetArrayItem(data_obj, idx);

                strcpy(obj.bronze, cJSON_GetObjectItem(list_obj, "medal_bronze_count")->valuestring);
                strcpy(obj.rank, cJSON_GetObjectItem(list_obj, "rank")->valuestring);
                strcpy(obj.count, cJSON_GetObjectItem(list_obj, "medal_sum_count")->valuestring);
                strcpy(obj.silver, cJSON_GetObjectItem(list_obj, "medal_silver_count")->valuestring);
                strcpy(obj.countryname, cJSON_GetObjectItem(list_obj, "country_name")->valuestring);
                strcpy(obj.gold, cJSON_GetObjectItem(list_obj, "medal_gold_count")->valuestring);
                strcpy(obj.update_time, cJSON_GetObjectItem(list_obj, "update_time")->valuestring);
                printf("%s: %s %s %s=%s-%s-%s\n",
                       pobj->rank, pobj->update_time, pobj->countryname,
                       pobj->count, pobj->gold, pobj->silver, pobj->bronze);
            }
        } else return;
    }else return;

    cJSON_Delete(root);
    return;
}

void parse_cntv_data(char *str)
{
    cJSON *root, *data_obj, *medalsList_obj, *list_obj;
    root = cJSON_Parse((const char*)str);

    medalObj *pobj;
    medalObj obj;
    pobj = &obj;

    if(root != 0)
    {
        printf("JSON format ok, start parse!!!\r\n");
        data_obj = cJSON_GetObjectItem(root, "data");
        if(data_obj->type == cJSON_Object)
        {
            medalsList_obj = cJSON_GetObjectItem(data_obj, "medalsList");
            if(medalsList_obj->type == cJSON_Array)
            {
                int size = cJSON_GetArraySize(medalsList_obj);
                for(int idx = 0; idx < size; idx++)
                {
                    list_obj = cJSON_GetArrayItem(medalsList_obj, idx);

                    strcpy(obj.bronze, cJSON_GetObjectItem(list_obj, "bronze")->valuestring);
                    strcpy(obj.rank  , cJSON_GetObjectItem(list_obj, "rank")->valuestring);
                    strcpy(obj.count , cJSON_GetObjectItem(list_obj, "count")->valuestring);
                    strcpy(obj.silver, cJSON_GetObjectItem(list_obj, "silver")->valuestring);
                    strcpy(obj.countryname, cJSON_GetObjectItem(list_obj, "countryname")->valuestring);
                    strcpy(obj.gold       , cJSON_GetObjectItem(list_obj, "gold")->valuestring);
                    strcpy(obj.countryid  , cJSON_GetObjectItem(list_obj, "countryid")->valuestring);

                    printf("%s-%s-%s-%s-%s-%s\n",
                         pobj->rank, pobj->countryid, pobj->count,
                           pobj->gold, pobj->silver, pobj->bronze);

                }
            } else return;
        } else return;
    }else return;

    cJSON_Delete(root);
    return;
}
